package com.pxmeta.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pxmeta.chat.entity.dto.SetDisturbDTO;
import com.pxmeta.chat.service.HuanxinService;
import com.pxmeta.common.core.domain.AjaxResult;
import com.pxmeta.common.exception.ServiceException;
import com.pxmeta.user.entity.UserBlock;
import com.pxmeta.user.entity.UserDisturb;
import com.pxmeta.user.mapper.UserBlockMapper;
import com.pxmeta.user.mapper.UserDisturbMapper;
import com.pxmeta.user.service.IUserBlockService;
import com.pxmeta.user.service.IUserDisturbService;
import com.pxmeta.user.service.IUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wcl
 * @since 2022-07-13
 */
@Service
@RequiredArgsConstructor
public class UserDisturbServiceImpl extends ServiceImpl<UserDisturbMapper, UserDisturb> implements IUserDisturbService {

    /**
     * 屏蔽用户
     * @param userId
     * @param dto
     */
    @Override
    public void disturbCircle(Long userId, SetDisturbDTO dto) {
        disturb(dto.getEnabled(),userId, dto.getTargetCircleId(),2);
    }

    /**
     * 屏蔽群聊
     * @param userId
     * @param dto
     */
    @Override
    public void disturbChatGroup(Long userId, SetDisturbDTO dto) {
        disturb(dto.getEnabled(),userId, dto.getChatGroupId(),1);
    }



    private void disturb(boolean enabled, Long userId, Long targetId, Integer type){
        if (!enabled){
            LambdaQueryWrapper<UserDisturb> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(UserDisturb::getUserId, userId)
                    .eq(UserDisturb::getTargetId,targetId)
                    .eq(UserDisturb::getTargetType, type);
            UserDisturb disturb = this.getOne(wrapper);
            if (null != disturb){
                this.removeById(disturb);
            }
            return;
        }
        UserDisturb userDisturb = new UserDisturb();
        userDisturb.setUserId(userId);
        userDisturb.setTargetId(targetId);
        userDisturb.setTargetType(type);
        this.save(userDisturb);
    }
}
